package com.yifushidai.qrcode;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

//import com.sun.org.apache.xml.internal.security.utils.Base64;

/**
 * <p>
 * BASE64编码解码工具包
 * </p>
 * <p>
 * 依赖javabase64-1.3.1.jar
 * </p>
 *
 * @author wxw
 * @version 1.0
 * @date 2017-05-16
 */
public class Base64Util {

    /**
     * 文件读取缓冲区大小
     */
    private static final int CACHE_SIZE = 1024;

    /**
     * <p>
     * BASE64字符串解码为二进制数据
     * </p>
     *
     * @param base64
     * @return
     * @throws Exception
     */
    public static byte[] decode(String base64) throws Exception {
        return Base64.decodeBase64(base64.getBytes());
    }

    /**
     * <p>
     * 二进制数据编码为BASE64字符串
     * </p>
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(byte[] bytes) throws Exception {
        return new String(Base64.encodeBase64(bytes));
    }

    /**
     * <p>
     * 将文件编码为BASE64字符串
     * </p>
     * <p>
     * 大文件慎用，可能会导致内存溢出
     * </p>
     *
     * @param filePath 文件绝对路径
     * @return
     * @throws Exception
     */
    public static String file2Base(String filePath) {
        byte[] bytes;
        try {
            bytes = file2Byte(filePath);
            return encode(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <p>
     * BASE64字符串转回文件
     * </p>
     *
     * @param filePath 文件绝对路径
     * @param base64   编码字符串
     * @throws Exception
     */
    public static void base2File(String base64, String filePath) throws Exception {
        byte[] bytes = decode(base64);
        byte2File(bytes, filePath);
    }

    /**
     * <p>
     * 文件转换为二进制数组
     * </p>
     *
     * @param filePath 文件路径
     * @return
     * @throws Exception
     */
    public static byte[] file2Byte(String filePath) throws Exception {
        byte[] data = new byte[0];
        File file = new File(filePath);
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }
            out.close();
            in.close();
            data = out.toByteArray();
        }
        return data;
    }

    /**
     * <p>
     * 二进制数据写文件
     * </p>
     *
     * @param bytes    二进制数据
     * @param filePath 文件生成目录
     */
    public static void byte2File(byte[] bytes, String filePath) throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        destFile.createNewFile();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);
            out.flush();
        }
        out.close();
        in.close();
    }

    public static String encodeReverse(String source) throws Exception {
        String encodeStr = Base64Util.encode(source.getBytes());
        encodeStr = new StringBuilder(encodeStr).reverse().toString();
        encodeStr = Base64Util.encode(encodeStr.getBytes());
        encodeStr = new StringBuilder(encodeStr).reverse().toString();
        return encodeStr;
    }

    public static String decodeReverse(String source) throws Exception {
        String decodeStr = new StringBuilder(source).reverse().toString();
        decodeStr = new String(Base64Util.decode(decodeStr));
        decodeStr = new StringBuilder(decodeStr).reverse().toString();
        decodeStr = new String(Base64Util.decode(decodeStr));
        return decodeStr;
    }

    public static void main(String[] args) throws Exception {
        String source = "123";
        String SALT = "今天你吃了么?";
        String encodeStr = Base64Util.encode((source + SALT).getBytes());
        System.out.println(encodeStr);
        System.out.println(Base64Util.decode(encodeStr));

        String encode = encodeReverse(SALT);
        System.out.println(encode);
        String decode = decodeReverse(encode);
        System.out.println(decode);
    }
}